/*****************************************************************************

File:   otkLayerStateUtils.java

Date          Build     Author        Version    Comments
18-Sep-12    P-20-14    dshah          $$1        Created
08-Oct-12    P-20-14    dshah          $$2        Updated
12-Oct-12    P-20-14    dshah          $$3        Updated
30-Oct-12    P-20-16    dshah          $$4        Updated
20-mar-15 P-30-05 gshmelev $$5  called GetCurrentSession

****************************************************************************/

/* 
   
Utilites for layer state.

*/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFeatureInstructions.*;
import com.ptc.pfc.pfcSelect.*;
import com.ptc.wfc.wfcSession.*;
import com.ptc.pfc.pfcUI.*;
import com.ptc.pfc.pfcExceptions.*;
import com.ptc.pfc.pfcXSection.*;
import com.ptc.pfc.pfcObject.*;
import com.ptc.wfc.wfcCombState.*;
import com.ptc.wfc.wfcXSection.*;
import com.ptc.wfc.wfcLayerState.*;
import com.ptc.pfc.pfcLayer.*;
import com.ptc.wfc.wfcModelItem.*; 
import com.ptc.pfc.pfcSimpRep.*;
import com.ptc.wfc.wfcAssembly.*;
import com.ptc.pfc.pfcBase.*;

 

class otkLayerStateUtils extends DefaultUICommandActionListener
{
    

    
    public otkLayerStateUtils()
    {
    }
    
    // Utility to select layer item from layer
    public static LayerItem otkUtilSelectLayerItemfromLayer(Layer WLayer, int ItemNum)
    {
	try
	    {	
		Session session = pfcSession.GetCurrentSession ();
		ModelItems WLayerItems = WLayer.ListItems ();
		int ItemsSize = WLayerItems.getarraysize ();
		
		if(ItemsSize > 0)
		    {
			if(ItemNum != -1 &&(ItemNum > 0 && ItemNum <= ItemsSize))
			    {
				LayerItem WSelectedLayerItem = (LayerItem)(WLayerItems.get(ItemNum -1));
				return (WSelectedLayerItem);
			    }
			// Select from UI
			else
			    {
				String buff;
				String line;
				stringseq msgs = stringseq.create();
				
				line = "Layer item: ";
				for (int i = 0; i < ItemsSize; i++)
				    {
					//pfcModelItem_ptr WLayerItem = WLayerItems->get(i);  
					LayerItem WLayerItem = (LayerItem)(WLayerItems.get(i));
					
					ModelItem modelItem = (ModelItem)(WLayerItem);
					if(modelItem.GetName() != null)
					    {
						if(i+1 < ItemsSize)
						    buff = " " + (i+1) + ": " +  modelItem.GetName() + ", ";
						else
						    buff = " " + (i+1) + ": " +  modelItem.GetName() + ", ";
						
						line += buff;
					    }
					
				    }
				msgs.append(line);
				session.UIDisplayMessage("layer_state.txt", "USER Enter number to select %0s", msgs);
				Integer SelectedLayerNo = session.UIReadIntMessage(1, ItemsSize);
				if(SelectedLayerNo.intValue() > 0 && SelectedLayerNo.intValue() <= ItemsSize)
				    {
					//pfcModelItem_ptr WSelectedLayerItem = WLayerItems->get(SelectedLayerNo -1);
					LayerItem WSelectedLayerItem = (LayerItem)(WLayerItems.get(SelectedLayerNo.intValue()-1));
					return (WSelectedLayerItem);
				    }
			    }
		    }
		
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (null);
    }
    
    // Utility to return layer from layerstate
    // If LayerNum == -1 then input will be taken through UI
    //*********************************************
    public static Layer otkUtilSelectLayerfromLayerState (LayerState WLayerState, int LayerNum)
    {
	try
	    {	
		Session session = pfcSession.GetCurrentSession ();
		
		// Getting LayerStateData from LayerState
		LayerStateData WLayerData = WLayerState.GetLayerData();
		if(WLayerData != null)
		    {
			// Getting Layers from LayerStateData
			Layers WLayers = WLayerData.GetLayers ();
			int layersSize = WLayers.getarraysize ();
			
			if(layersSize > 0)
			    {
				if(LayerNum != -1 && (LayerNum > 0 && LayerNum <= layersSize))
				    {
					Layer WSelectedLayer = WLayers.get(LayerNum-1);
					return (WSelectedLayer);
				    }
				// Select from UI
				else
				    {
					String buff;
					String line;
					stringseq msgs = stringseq.create();
					
					line = "Layer: ";
					for (int i = 0; i < layersSize; i++)
					    {
						Layer WLayer = WLayers.get(i);
						if(WLayer != null)
						    {
							if(i+1 < layersSize)
							    buff = " " + (i+1) + ": " +  WLayer.GetName() + ", ";
							else
							    buff = " " + (i+1) + ": " +  WLayer.GetName() + ", ";
							
							line += buff;
						    }
					    }
					msgs.append(line);
					session.UIDisplayMessage("layer_state.txt", "USER Enter number to select %0s", msgs);
					Integer SelectedLayerNo = session.UIReadIntMessage(1, layersSize);
					if(SelectedLayerNo.intValue() > 0 && SelectedLayerNo.intValue() <= layersSize)
					    {
						Layer WSelectedLayer = WLayers.get(SelectedLayerNo.intValue()-1);
						return (WSelectedLayer);
					    }
				    }
			    }
		    }
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (null);
    }
    
    // Utility to select one of the associated layer for given layer item
    //*********************************************
    public static Layer otkUtilSelectAssociatedLayer (LayerItem WLayerItem)
    {
	try
	    {	
		Session session = pfcSession.GetCurrentSession ();
		
		Layers  WLayers = WLayerItem.GetLayers(null);
		int NoOfLayers = WLayers.getarraysize ();
		
		if(NoOfLayers == 1)
		    return (WLayers.get(0));
		else if(NoOfLayers > 0)
		    {
			String buff;
			String line;
			stringseq msgs = stringseq.create();
			
			line = "Layer: ";
			for (int i = 0; i < NoOfLayers; i++)
			    {
				Layer WLayer = WLayers.get(i);
				if(WLayer != null)
				    {
					if((i+1) < NoOfLayers)
					    buff = " " + (i+1) + ": " +  WLayer.GetName() + ", ";
					else
					    buff = " " + (i+1) + ": " +  WLayer.GetName() + ", ";
					
					line += buff;
				    }
			    }
			msgs.append(line);
			session.UIDisplayMessage("layer_state.txt", "USER Enter number to select %0s", msgs);
			Integer SelectedLayerNo = session.UIReadIntMessage(1, NoOfLayers);
			if(SelectedLayerNo.intValue() > 0 && SelectedLayerNo.intValue() <= NoOfLayers)
			    {
				Layer WSelectedLayer = WLayers.get(SelectedLayerNo.intValue()-1);
				return (WSelectedLayer);
			    }
		    }
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (null);
    }
    
    // Utility to check layer (using I/P layer name) in soild 
    // and if exist returns that layer else returns NULL
    //*********************************************
    public static Layer otkUtilGetLayerExist(WSolid WSolid, String LayerNamePassed)
    {
	try
	    {
		Layers WmodelLayers = WSolid.ListLayers();
		
		int WLayersSize = WmodelLayers.getarraysize ();
		for(int i = 0; i < WLayersSize; i++)
		    {
			Layer WExistingLayer = WmodelLayers.get(i);
			String ExisintgLayerName = WExistingLayer.GetName();
			
			boolean stringCheck = LayerNamePassed.equalsIgnoreCase(ExisintgLayerName);
			
			if(stringCheck == true)
			    return (WExistingLayer);
		    }
		return (null);
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (null);
    }
    
    // Utility t0 return LayerState from input solid
    // Pass StateNum = -1 to select layer state through UI
    //*********************************************
    public static LayerState otkUtilSelectLayerState (WSolid WSolid, int StateNum)
    {
	try
	    {	
		Session session = pfcSession.GetCurrentSession ();
		
		LayerStates WLayerStates =   WSolid.ListLayerStates();
		if(WLayerStates == null)
		    return null;
		
		int LayerStatesArraySize = WLayerStates.getarraysize ();
		if(LayerStatesArraySize > 0)
		    {
			if(StateNum != -1 &&(StateNum > 0&& StateNum <= LayerStatesArraySize))
			    {
				LayerState WLayerState = WLayerStates.get(StateNum -1);
				return (WLayerState);
			    }
			// Select from UI
			else
			    {
				String buff;
				String line;
				stringseq msgs = stringseq.create();
				
				line = "Layer States: ";
				for (int i = 0; i < LayerStatesArraySize; i++)
				    {
					LayerState WLayerState = WLayerStates.get(i);
					if(WLayerState != null)
					    {
						if(i+1 < LayerStatesArraySize)
						    buff = " " + (i+1) + ": " +  WLayerState.GetName() + ", ";
						else
						    buff = " " + (i+1) + ": " +  WLayerState.GetName() + ", ";
						
						line += buff;
					    }
				    }
				msgs.append(line);
				session.UIDisplayMessage("layer_state.txt", "USER Enter number to select %0s", msgs);
				Integer SelectedLayerStateNo = session.UIReadIntMessage(1, LayerStatesArraySize);
				if(SelectedLayerStateNo.intValue() > 0 && SelectedLayerStateNo.intValue() <= LayerStatesArraySize)
				    {
					LayerState WLayerState = WLayerStates.get(SelectedLayerStateNo.intValue()-1);
					return (WLayerState);
				    }
			    }
		    }
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (null);
    }
    
    // Creates new layerstate for current layers in UI
    //*********************************************
    public static LayerState OTKUtilCreateLayerState (WSolid WSolid, String wLayerStateName)
    {
	try
	    {		
		Session session = pfcSession.GetCurrentSession ();
		ModelType modelType = WSolid.GetType();
		
		
		if(wLayerStateName.length() != 0 && wLayerStateName != null)
		    {
			Layers WmodelLayers = WSolid.ListLayers();
			//Adds Layer State item
			int WLayersSize = WmodelLayers.getarraysize ();
			if(WLayersSize > 0)
			    {
				DisplayStatuses wDisplayStatuses = DisplayStatuses.create();
				LayerItems wLayerItems = LayerItems.create();
				for (int i = 0; i < WLayersSize; i++)
				    {
					// Adds layers display status
					Layer WmodelLayer = WmodelLayers.get(i);
					if(WmodelLayer != null)
					    {
						wDisplayStatuses.append(WmodelLayer.GetStatus());
					    }
					
					// Adds hidden layer items
					ModelItems PfcLayerItems = WmodelLayer.ListItems();
					int WLayerItemsSize = PfcLayerItems.getarraysize ();
					for (int j = 0; j < WLayerItemsSize; j++)
					    {
						WModelItem WMdlItem = (WModelItem)(PfcLayerItems.get(j));
						boolean IsHidden = WMdlItem.IsHidden();
						if(IsHidden == true)
						    {
							wLayerItems.append((LayerItem)(WMdlItem));
						    }
					    }
				    }
				// Creates  new layer state
				LayerStateData wLayerStateData = wfcLayerState.LayerStateData_Create(WmodelLayers, wDisplayStatuses, wLayerItems, wLayerStateName);
				LayerState WLayerState = WSolid.CreateLayerState(wLayerStateData);
				
				// Activates created layer state
				if(WLayerState != null)
				    WLayerState.ActivateLayerState();
				return WLayerState;
			    }
		    }
		return null;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return null;
    }
    
    // Utility to Update layerstate
    // If updateAct = -1, LayerName = xstringnil,  ItemNum = -1 and ItemAction = -1 then input will be taken through UI
    //*********************************************
    public static int otkUtilUpdateLayerState (WSolid WSolid, LayerState WLayerState, 
						int updateAct,  String LayerName, int ItemNum, 
						int ItemAction)
    {
	try
	    {
		Session session = pfcSession.GetCurrentSession ();
		String LayerNameToUse;
		Integer SelectedLayerAction = new Integer(-1);
		
		if(updateAct != -1 && (updateAct > 0 && updateAct <= 3))
		    SelectedLayerAction = new Integer(updateAct);
		// Take input from UI
		else
		    {
			session.UIDisplayMessage("layer_state.txt", "USER 1: Add Layer, 2: Remove Layer, 3: Layer item actions", null);
			SelectedLayerAction = session.UIReadIntMessage(1, 3);
		    }
		
		Layer WLayer = null;
		if(LayerName != null)
		    {
			LayerNameToUse = LayerName;
			WLayer = otkUtilGetLayerExist(WSolid, LayerNameToUse);
			if(WLayer == null)
			    return -1;
		    }
		
		if(SelectedLayerAction.intValue() > 0 && SelectedLayerAction.intValue() <= 3)
		    {
			switch(SelectedLayerAction.intValue())
			    {
				// Adds Layer
			    case 1: 
				{	
				    // Take input from UI
				    if(LayerName == null)
					{
					    session.UIDisplayMessage("layer_state.txt", "USER Enter layer name: ", null);
					    LayerNameToUse = session.UIReadStringMessage(null);
					    
					    WLayer = otkUtilGetLayerExist(WSolid, LayerNameToUse);
					}
				    if(WLayer != null)
					{
					    DisplayStatus DispStatus = WLayer.GetStatus();
					    WLayerState.AddLayer(WLayer, DispStatus);
					}
				    break;
				}
				
				//Removes Layer
			    case 2:
				{
				    // Take input from UI
				    if(LayerName == null)
					{
					    session.UIDisplayMessage("layer_state.txt", "USER Enter layer name: ", null);
					    LayerNameToUse = session.UIReadStringMessage(null);
					    
					    WLayer = otkUtilGetLayerExist(WSolid, LayerNameToUse);
					}
				    if(WLayer != null)
					{
					    DisplayStatus DispStatus = WLayer.GetStatus();
					    WLayerState.RemoveLayer(WLayer);
					}
				    break;
				}
				
				//Layer item actions
			    case 3:
				{
				    LayerItem  WSelectedLayerItem;
				    ModelItem  pSelectedLayerItem;
				    
				    if(ItemNum != -1 && WLayer != null)
					{
					    WSelectedLayerItem = otkUtilSelectLayerItemfromLayer(WLayer, ItemNum);
					    pSelectedLayerItem = (ModelItem)(WSelectedLayerItem); 
					    if(pSelectedLayerItem == null)
						return 0;
					}
				    // Select layer item through UI
				    else
					{
					    Integer num = new Integer(1);
					    
					    SelectionOptions WSelopts = pfcSelect.SelectionOptions_Create("feature, datum, csys");
					    WSelopts.SetMaxNumSels(num);	 
					    
					    Selections Sels = session.Select(WSelopts, null);
					    if (Sels == null)
						return 0;
					    
					    pSelectedLayerItem = (Sels.get(0)).GetSelItem();
					    if(pSelectedLayerItem == null)
						return 0;
					    WSelectedLayerItem = (LayerItem)(pSelectedLayerItem);
					}	
				    
				    if(WSelectedLayerItem != null)
					{
					    Integer SelectedItemAction = new Integer(-1);
					    if(ItemAction != -1)
						SelectedItemAction = new Integer(ItemAction);
					    // Select from UI
					    else
						{
						    session.UIDisplayMessage("layer_state.txt", "USER 1: Hide, 2: Unhide, 3: RemoveNoUpdate, 4: AddNoUpdate", null);
						    SelectedItemAction = session.UIReadIntMessage(1, 4);
						}
					    if(SelectedItemAction.intValue() > 0 && SelectedItemAction.intValue() <= 4)
						{
						    switch(SelectedItemAction.intValue())
							{
							    // Hide Layer item
							case 1: 
							    {
								WLayerState.HideLayerItem( pSelectedLayerItem );
								break;
							    }
							    // Unhide Layer item
							case 2: 
							    {
								WLayerState.UnhideLayerItem( pSelectedLayerItem );
								break;
							    }
							    // RemoveNoUpdate Layer for layer item
							case 3: 
							    {
								if(WLayer != null)
								    WSelectedLayerItem.RemoveNoUpdate(WLayer);
								//Select layer from UI
								else
								    {
									WLayer = otkUtilSelectAssociatedLayer(WSelectedLayerItem);
									if(WLayer == null)
											return 0;
									WSelectedLayerItem.RemoveNoUpdate(WLayer);
								    }
								break;
							    }
							    // AddNoUpdate Layer for layer item
							case 4: 
							    {
								if(WLayer != null)
								    WSelectedLayerItem.AddNoUpdate(WLayer);
								//Select layer from UI
								else
								    {
									
									
										session.UIDisplayMessage("layer_state.txt", "USER Enter layer name: ", null);
										LayerNameToUse = session.UIReadStringMessage(null);
					    
										WLayer = otkUtilGetLayerExist(WSolid, LayerNameToUse);
										if(WLayer != null)
										{
											WSelectedLayerItem.AddNoUpdate(WLayer);
										}					
								    }
								break;
							    }
							}
						}
					}
				    break;
				}
			    }
		    }
		return (0);
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (0);
    }
    
	// Utility to dump layerstate info
	//*********************************************
    public static int otkUtilLayerStateInfo (WSolid WSolid, LayerState WLayerState, BufferedWriter fp_out_layerstateInfo)
    {
	String line;
	String buff;
	try
	    {
		fp_out_layerstateInfo.write("==============================");
		fp_out_layerstateInfo.newLine();
		fp_out_layerstateInfo.write( "- Layer State Info -");
		fp_out_layerstateInfo.newLine();
		fp_out_layerstateInfo.write("==============================");
		fp_out_layerstateInfo.newLine();
		
		// Getting GetLayerState Name from LayerState
		String  WStateName = WLayerState.GetLayerStateName();
		buff = " - Layer State Name: " + WStateName + ", ID: " + WLayerState.GetId() + ",";
		line = buff;
		
		// Getting LayerStateData from LayerState
		LayerStateData WLayerData = WLayerState.GetLayerData();
		if(WLayerData != null)
		    {
			// Getting DisplayStatuses from LayerStateData
			DisplayStatuses WDisplayStatuses =  WLayerData.GetDisplayStatuses();
			int WDisplayStatusesSize = WDisplayStatuses.getarraysize ();
			buff = " Number of DisplayStatuses: " + WDisplayStatusesSize + ",";
			line += buff;
			
			// Getting Layers from LayerStateData
			Layers WLayers = WLayerData.GetLayers ();
			int layersSize = WLayers.getarraysize ();
			//sprintf(buff, "Number of layers: %d",layersSize);
			buff = " Number of layers: " + layersSize;
			line += buff;
			fp_out_layerstateInfo.write(line);
			fp_out_layerstateInfo.newLine();
			
			// Getting layer info
			for (int i = 0; i < layersSize; i++)		
			    {
				Layer WLayer = WLayers.get(i);
				buff = "\t- Layer " + (i+1) + " ID: " + WLayer.GetId() + ", Name: " + WLayer.GetName() + ",";
				line = buff;

				// Getting layers dicplay status
				DisplayStatus DispStatus = WLayer.GetStatus();
				switch(DispStatus.getValue())
				    {
				    case 0: 
					line += " status: LAYER_NORMAL,";
					break;
					
				    case 1:
					line += " status: LAYER_DISPLAY,";
					break;
					
				    case 2:
					line += " status: LAYER_BLANK,";
					break;
					
				    case 3:
					line += " status: LAYER_HIDDEN,";
					break;
				    }
				ModelItems WLayerItems = WLayer.ListItems ();
				int LayerItemsSize = WLayerItems.getarraysize ();
				buff = " Number of items: " + LayerItemsSize;
				
				line += buff;
				fp_out_layerstateInfo.write(line);
				fp_out_layerstateInfo.newLine();
				
				for (int k = 0; k < LayerItemsSize; k++)
				    {
					LayerItem WLayerItem  =  (LayerItem)(WLayerItems.get(k));
					// another approach to casting: instead of calling isObjKindOf, 
					// we do the cast without any checking - it will produce null if not applicable
					ModelItem modelItem = (ModelItem)(WLayerItem); 
					if (modelItem != null)
					    {
						String WLayerItemName = modelItem.GetName();
						buff = "\t\t - Item" +(k+1) + " ID: " + modelItem.GetId() + ",";
						line = buff;
						
						if(WLayerItemName.length() != 0 && WLayerItemName != null)
						    {
							buff = " Name: " + WLayerItemName + ",";
							line += buff;
						    }
						// Getting LayerItems Hidden status
						boolean IsItemHidden = WLayerState.IsLayerItemHidden(modelItem);
						line += (IsItemHidden == true) ? " Hidden,":" Not hidden,";
					    }
					
					// Getting LayerItems Visible status
					boolean IsItemVisible = WLayerItem.IsLayerItemVisible(null);
					line += (IsItemVisible == true) ? " Visible, ":" Not visible, ";
					
					// Getting number of layers associated with layer item
					Layers   LayersWithItem = WLayerItem.GetLayers(null);
					int NoOfLayers = LayersWithItem.getarraysize ();
					buff = "Associated layers: " + NoOfLayers;
					line += buff;
					fp_out_layerstateInfo.write(line);
					fp_out_layerstateInfo.newLine();
				    }
				fp_out_layerstateInfo.newLine();
			    }
			
			// Getting Hidden LayerItems from LayerStateData
			LayerItems WLayerItems  =  WLayerData.GetLayerItems ();
			int LayerItemsSize = WLayerItems.getarraysize ();
			fp_out_layerstateInfo.write(" - Hidden LayerItems in this layer state: " + LayerItemsSize);
			fp_out_layerstateInfo.newLine();
			
			for (int i = 0; i < LayerItemsSize; i++)
			    {
				LayerItem WLayerItem  =  WLayerItems.get(i);
				ModelItem modelItem = (ModelItem)(WLayerItem);
				if (modelItem != null)
				    {
					buff = "\t\t - Hidden Item" + (i+1) + " ID: " + modelItem.GetId() + ", ";
					line = buff;
					
					String WLayerItemName = modelItem.GetName();
					if(WLayerItemName.length() != 0 && WLayerItemName != null)
					    {
						buff = "Name: " + WLayerItemName + ",";
						line += buff;
					    }
				    }
				
				// Getting LayerItems Visible status
				boolean IsItemVisible = WLayerItem.IsLayerItemVisible(null);
				line += (IsItemVisible == true) ? " Visible, ":" Not visible, ";
				
				// Getting number of layers associated with layer item
				Layers   LayersWithItem = WLayerItem.GetLayers(null);
				int NoOfLayers = LayersWithItem.getarraysize ();
				buff = "Number of Layers associated: " + NoOfLayers;
				line += buff;
				fp_out_layerstateInfo.write(line);
				fp_out_layerstateInfo.newLine();
				
				LayerState WActiveLayerState = WSolid.GetActiveLayerState();
				if(WActiveLayerState!= null)
				    {
					fp_out_layerstateInfo.write(" - Current active Layer State: " + WActiveLayerState.GetLayerStateName());
				    }
			    }
		    }
		return (0);
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (0);
	}
    
    // Utility to delete layerstate
    //*********************************************
    public static int otkUtilDeleteLayerState (LayerState WLayerState)
    {
	try
	    {
		WLayerState.DeleteLayerState();
		return (0);
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
		return (0);
    }
	
    
};






